package persistence.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import persistence.db.DataSet;
import persistence.db.DatabaseConnector;
import persistence.model.AbstractModel;
import persistence.model.Salle;

public class SalleDAO extends AbstractDAO{
	
	private Salle salle;

	/**
	 * Constructeur d'une salle DAO
	 */
	public SalleDAO() {
		setTableName("SALLE");
	}
	
	/**
	 * Constructeur d'une salle DAO
	 * @param s salle correspondante
	 */
	public SalleDAO(Salle s){
		this();
		salle = s;
		setId(salle.getId());
	}
	
	public Salle getSalle() {
		return salle;
	}

	public void setSalle(Salle salle) {
		this.salle = salle;
	}

	public DataSet all() {
		return new DataSet(this);
	}

	public AbstractModel makeModel(ResultSet rset) {
		try {
			setId(rset.getInt("id_Salle"));
			return new Salle(getId(),
					rset.getString("nomSalle"),
					rset.getInt("capaciteSalle"));
		} catch (SQLException e) {
			System.out.println("makeModel error");
			e.printStackTrace();
		}
		return null;
	}

	public void save() {
		String query;
		ResultSet result;
			query = "INSERT INTO " + getTableName() + " VALUES ( S_PK_"+ getTableName() +".nextval, '" +
			salle.getNomSalle() + "', " + salle.getCapaciteSalle() + ")";
			/* Execute la requete */
			result = DatabaseConnector.getInstance().execute(query);
			setId(this.getId()); /* Mise a jour de l'identifiant */
	}

	public void save(int id) {
		String query;
		ResultSet result;
			query = "UPDATE " + getTableName() +
			" SET nomSalle = '" + salle.getNomSalle() +
			"', capaciteSalle = " + salle.getCapaciteSalle() +
			" WHERE ID_"+ getTableName() + " = "+ id;
			/* Execute la requete */
			result = DatabaseConnector.getInstance().execute(query);
	}
}
